package com.hoshiicloud.user.service.impl.organization;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.entity.company.AttachmentType;
import com.hoshiicloud.user.entity.organization.StoreOrganization;
import com.hoshiicloud.user.entity.organization.StoreOrganizationAttachment;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.organization.StoreOrganizationAttachmentMapper;
import com.hoshiicloud.user.service.company.IAttachmentTypeService;
import com.hoshiicloud.user.service.organization.StoreOrganizationAttachmentService;
import com.hoshiicloud.user.service.organization.StoreOrganizationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 店铺供应商附件 服务实现类
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Service
public class StoreOrganizationAttachmentServiceImpl extends ServiceImpl<StoreOrganizationAttachmentMapper, StoreOrganizationAttachment> implements StoreOrganizationAttachmentService {
    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private StoreOrganizationService storeOrganizationService;
    @Autowired
    private IAttachmentTypeService attachmentTypeService;
    @Autowired
    private StoreOrganizationAttachmentMapper storeOrganizationAttachmentMapper;


    /**
     * @return IPage
     * @description: 分页查询店铺供应商附件
     */
    @Override
    @Logable(authName = "供应商信息-店铺供应商附件管理-分页查询店铺供应商附件")
    @Cacheable(cacheNames = "StoreOrganizationAttachment",key ="#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public IPage<StoreOrganizationAttachment> getPage(IPage page, QueryWrapper queryWrapper,String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
    * @param id
    * @return com.hoshiicloud.organization.entity.StoreOrganizationAttachment
    * @description: 根据id查询店铺供应商附件
    */
    @Override
    @Cacheable(cacheNames = "StoreOrganizationAttachment", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "供应商信息-店铺供应商附件 -查询店铺供应商附件 ")
    public StoreOrganizationAttachment getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增店铺供应商附件
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreOrganizationAttachment", allEntries = true)
    @Logable(authName = "供应商信息-店铺供应商附件-新增店铺供应商附件")
    public boolean save(StoreOrganizationAttachment entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        StoreOrganization storeOrganization = storeOrganizationService.getById(entity.getVendorId());
        if(null == storeOrganization){
            throw new ServiceException(CommonError.error("新增失败,店铺（伙伴）供应商信息有误"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改店铺供应商附件
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreOrganizationAttachment", allEntries = true)
    @Logable(authName = "供应商信息-店铺供应商附件管理-修改店铺供应商附件")
    public boolean updateById(StoreOrganizationAttachment entity) {
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
        throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
     *@Author:campchen
     *@Description: 批量新增机构附件store_organization_attach
     *@Date:上午 09:36 2019/8/30
     *@Param:
     *@return:
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreOrganizationAttach", allEntries = true)
    @Logable(authName = "供应商信息-机构附件store_organization_attach管理-批量新增机构附件store_organization_attach")
    @Override
    public boolean insertAttach4StoreOrganization(List<StoreOrganizationAttachment> storeOrganizationAttachmentList) {
        for (int i=0;i<storeOrganizationAttachmentList.size();i++){
            storeOrganizationAttachmentList.get(i).setCreatedTime(LocalDateTime.now());
        }
        return storeOrganizationAttachmentMapper.insertAttach4StoreOrganization(storeOrganizationAttachmentList);
    }
}
